

#yield实现生成器
#yield实现协程
# def fun1():
#     r=''
#     while 1:
#         n = yield r
#         r=f"任务一在执行{n}"
#
#
# def fun2():
#     r=''
#     while 1:
#         n = yield r
#         r = f"任务二在执行{n}"
#
#
# if __name__ == '__main__':
#     f1 = fun1()
#     f2 = fun2()
#     f1.send(None)
#     f2.send(None)
#     print(f1.send(123))
#     print(f2.send(321))
#     while 1:
#         print(f1.send(123))
#         print(f2.send(321))

#用greenlet实现协程
# 函数名.switch是在切换函数，从函数一切换到函数二，若切换的函数后还有代码则不执行
#就如下面的例子fun2的任务二在吃饭打印则不打印
# from greenlet import greenlet
#
# import time
#
# def fun1(name):
#
#     print(f"任务一在执行{name}")
#     time.sleep(1)
#     two.switch(321)
#     print(f"任务一吃饭{name}")
#
#
#
#
# def fun2(name):
#
#     print(f"任务二在执行{name}")
#     time.sleep(1)
#     one.switch()
#     print(f"任务二吃饭{name}")
#
#
#
# if __name__ == '__main__':
#     one = greenlet(fun1)
#     two = greenlet(fun2)
#     one.switch(123)


# import gevent
# import time
# from gevent import monkey
# monkey.patch_all()
# def eat(name):
#     print(f"{name}在吃饭")
#     time.sleep(2)
#     # gevent.sleep(2)
#     print(122333)
#
#
#
# def sleep(name):
#     print(f"{name}再睡觉")
#     # gevent.sleep(2)
#     time.sleep(2)
#
#     print(321145455)
#
#
# if __name__ == '__main__':
#     a = gevent.spawn(eat,'zengkun')
#     b = gevent.spawn(sleep,'zengkun')
#     a.join()
#     b.join()


import time

# 生成器
'''
    在一个函数中出现了yield，那么这个函数就称为生成器。
    每当执行到yield之后。就会在这暂停，需要等待下一个
    调用才会执行下面的代码。
    f1.send(None)为了让生成器停在yield这一行
'''
# def fun1():
#     # 返回参数
#     r = ''
#     while True:
#         # n=接收参数，r=返回值
#         n = yield r
#         r = f'任务一执行{n}'
#         # time.sleep(1)
#
# # 生成器
# def fun2():
#     # 返回参数
#     r = ''
#     while True:
#         # n=接收参数，r=返回值
#         n = yield r
#         r = f'任务二执行{n}'
#         # time.sleep(1)
#
#
# if __name__ == '__main__':
#     # 构建一个生成器
#     f1 = fun1()
#     # 启动生成器
#     f1.send(None)
#     f2 = fun2()
#     f2.send(None)
#
#     while True:
#         print(f1.send(123))
#         print(f2.send(321))

# from greenlet import greenlet
# import time
#
#
# def fun1():
#     while True:
#         print('任务一执行')
#         time.sleep(1)
#         er.switch()
#
#
# def fun2():
#     while True:
#         print('任务二执行')
#         time.sleep(1)
#         yi.switch()
#
#
# if __name__ == '__main__':
#     # 创建greenlet协程对象
#     yi = greenlet(fun1)
#     er = greenlet(fun2)
#     # 开启协程(同时也是对协程任务进行切换)
#     er.switch()


'''import gevent


def eat(name):
    print(f'{name}在吃饭')
    gevent.sleep(2)
    print(f'{name}又在吃饭')


def sleep1(name):
    print(f'{name}在睡觉')
    gevent.sleep(2)
    print(f'{name}又在睡觉')


if __name__ == '__main__':
    # 开启协程对象
    # spawn(任务, 参数)
    a = gevent.spawn(eat, '石头')
    b = gevent.spawn(sleep1, 'joker')
    # 让主程序等待协程任务执行完毕在退出
    # daemon 守护进程 子程序随着主程序关闭而关闭
    # join  主程序等待子程序执行完毕后再关闭
    a.join()
    b.join()'''

'''
import gevent
from gevent import monkey
import time

# 猴子补丁，普通的耗时操作（time.sleep(2)）使用
monkey.patch_all()

def eat(name):
    print(f'{name}在吃饭')
    time.sleep(2)
    print(f'{name}又在吃饭')


def sleep1(name):
    print(f'{name}在睡觉')
    time.sleep(2)
    print(f'{name}又在睡觉')


def da(name):
    print(f'{name}在打豆豆')
    time.sleep(2)
    print(f'{name}又在打豆豆')


if __name__ == '__main__':
    # 开启协程对象
    # spawn(任务, 参数)
    a = gevent.spawn(eat, '石头')
    b = gevent.spawn(sleep1, 'joker')
    c = gevent.spawn(da, 'tom')
    # 让主程序等待协程任务执行完毕在退出
    # daemon 守护进程 子程序随着主程序关闭而关闭
    # join  主程序等待子程序执行完毕后再关闭
    a.join()
    b.join()
    c.join()'''